﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace _7._2
{
    public partial class MainForm : Form
    {
        private readonly HttpClient _httpClient = new HttpClient();
        private const int MaxTextLength = 200;

        public MainForm()
        {
            InitializeComponent();
            _httpClient.Timeout = TimeSpan.FromSeconds(10);
        }

        private void InitializeComponent()
        {
            this.txtKeyword = new TextBox();
            this.btnSearch = new Button();
            this.txtBaidu = new TextBox();
            this.txtBing = new TextBox();
            this.SuspendLayout();

            // txtKeyword
            this.txtKeyword.Location = new System.Drawing.Point(20, 20);
            this.txtKeyword.Size = new System.Drawing.Size(300, 20);

            // btnSearch
            this.btnSearch.Location = new System.Drawing.Point(330, 18);
            this.btnSearch.Text = "搜索";
            this.btnSearch.Click += BtnSearch_Click;

            // txtBaidu
            this.txtBaidu.Multiline = true;
            this.txtBaidu.Location = new System.Drawing.Point(20, 60);
            this.txtBaidu.Size = new System.Drawing.Size(350, 200);
            this.txtBaidu.ScrollBars = ScrollBars.Vertical;

            // txtBing
            this.txtBing.Multiline = true;
            this.txtBing.Location = new System.Drawing.Point(380, 60);
            this.txtBing.Size = new System.Drawing.Size(350, 200);
            this.txtBing.ScrollBars = ScrollBars.Vertical;

            // MainForm
            this.ClientSize = new System.Drawing.Size(750, 280);
            this.Controls.Add(this.txtKeyword);
            this.Controls.Add(this.btnSearch);
            this.Controls.Add(this.txtBaidu);
            this.Controls.Add(this.txtBing);
            this.Text = "搜索引擎摘要提取";
            this.ResumeLayout(false);
        }

        private async void BtnSearch_Click(object sender, EventArgs e)
        {
            string keyword = txtKeyword.Text.Trim();
            if (string.IsNullOrEmpty(keyword))
            {
                MessageBox.Show("请输入搜索关键字");
                return;
            }

            btnSearch.Enabled = false;

            try
            {
                var tasks = new List<Task>
                {
                    SearchAndDisplay("百度", "https://www.baidu.com/s?wd=", txtBaidu, keyword),
                    SearchAndDisplay("必应", "https://www.bing.com/search?q=", txtBing, keyword)
                };

                await Task.WhenAll(tasks);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"搜索失败: {ex.Message}");
            }
            finally
            {
                btnSearch.Enabled = true;
            }
        }

        private async Task SearchAndDisplay(string engine, string baseUrl, TextBox textBox, string keyword)
        {
            try
            {
                string html = await _httpClient.GetStringAsync(baseUrl + Uri.EscapeDataString(keyword));
                string cleanText = CleanHtml(html);
                string result = cleanText.Length > MaxTextLength ?
                    cleanText.Substring(0, MaxTextLength) + "..." : cleanText;

                textBox.Invoke((MethodInvoker)delegate {
                    textBox.Text = $"{engine}搜索结果：\n\n{result}";
                });
            }
            catch
            {
                textBox.Invoke((MethodInvoker)delegate {
                    textBox.Text = $"{engine}搜索失败";
                });
            }
        }

        private string CleanHtml(string html)
        {
            // 简单HTML清理
            string text = Regex.Replace(html, @"<script.*?</script>", "",
                RegexOptions.Singleline | RegexOptions.IgnoreCase);
            text = Regex.Replace(text, @"<style.*?</style>", "",
                RegexOptions.Singleline | RegexOptions.IgnoreCase);
            text = Regex.Replace(text, @"<.*?>", " ");
            text = Regex.Replace(text, @"\s+", " ");
            return Encoding.UTF8.GetString(Encoding.Default.GetBytes(text)).Trim();
        }

        // Windows Forms控件声明
        private TextBox txtKeyword;
        private Button btnSearch;
        private TextBox txtBaidu;
        private TextBox txtBing;
    }


}
